package com.swallow.auth.infrastructure.mysql.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author: yangjie.deng@resico.cn
 * @since: 2024-03-31 11:15:03
 * @version: v1.0.0
 * @describe:
 */
public class MySQLGenerator {
    static final String userName = "root";

    static final String password = "123456";

    static final String url = "jdbc:mysql://192.168.213.50:3306/swallow_auth_dev?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL";

    public static void main(String[] args) {
        FastAutoGenerator.create(url, userName, password)
                .globalConfig(builder -> {
                    builder.author("18310740596@163.com") // 设置作者
                            .outputDir("E://mybatis-plus//swallow-auth"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT || typeCode == Types.TINYINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }

                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.swallow.auth.infrastructure") // 设置父包名
                            .moduleName("mysql")// 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "E://mybatis-plus//swallow-auth//xml")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                                    .enableLombok()
                                            .idType(IdType.AUTO);

                    builder.addInclude("swallow_auth_account","swallow_auth_account_rel_role","swallow_auth_account_rel_staff","swallow_auth_app","swallow_auth_department","swallow_auth_dept_rel_staff","swallow_auth_group_rel_role","swallow_auth_menu_permission","swallow_auth_role","swallow_auth_role_rel_permission","swallow_auth_staff","swallow_auth_tenant","swallow_auth_uri_permission","swallow_auth_user_group") // 设置需要生成的表名
                            .addTablePrefix("swallow_auth_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
